package com.lyn.view.scanning;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author:longyongning
 * @date:2021/10/6
 * @description:
 */
public class ScanningView extends View {

    private Paint paint;
    private int size = 500;
    private int angleSize = 50;
    private int angleLineSize = 10;
    private int angleColor=Color.GREEN;
    private int autoX = 0;
    private int speed = 3;
    private int scanningLineSize = 20;
    private int scanningLineColor=Color.GREEN;
    private boolean isScanning=false;

    /**
     * 设置扫描宽大小
     * @param size
     */
    public void setSize(int size) {
        this.size = size;
        postInvalidate();
    }

    /**
     * 设置角线长度大小
     * @param angleSize
     */
    public void setAngleSize(int angleSize) {
        this.angleSize = angleSize;
        postInvalidate();
    }

    /**
     * 设置角线粗细大小
     * @param angleLineSize
     */
    public void setAngleLineSize(int angleLineSize) {
        this.angleLineSize = angleLineSize;
        postInvalidate();
    }

    /**
     * 设置角线颜色
     * @param angleColor
     */
    public void setAngleColor(int angleColor) {
        this.angleColor = angleColor;
        postInvalidate();
    }

    /**
     * 设置扫描线扫描速度
     * @param speed
     */
    public void setSpeed(int speed) {
        this.speed = speed;
        postInvalidate();
    }

    /**
     * 设置扫描线粗细大小
     * @param scanningLineSize
     */
    public void setScanningLineSize(int scanningLineSize) {
        this.scanningLineSize = scanningLineSize;
        postInvalidate();
    }

    /**
     * 设置扫描线颜色
     * @param scanningLineColor
     */
    public void setScanningLineColor(int scanningLineColor) {
        this.scanningLineColor = scanningLineColor;
        postInvalidate();
    }

    /**
     * 设置扫描线是否扫描
     * @param scanning
     */
    public void setScanning(boolean scanning) {
        isScanning = scanning;
        postInvalidate();
    }

    public ScanningView(Context context) {
        super(context);
        init();
    }

    public ScanningView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        invalidate();
        int width = canvas.getWidth();
        int height = canvas.getHeight();

        float left = (width - size) / 2;
        float top = (height - size) / 2;
        float right = width - left;
        float bottom = height - top;
        paint.setColor(angleColor);
        paint.setStrokeWidth(angleLineSize);
        //左上角
        canvas.drawLine(left, top, left + angleSize, top, paint);
        canvas.drawLine(left, top, left, top + angleSize, paint);
        //右上角
        canvas.drawLine(right, top, right - angleSize, top, paint);
        canvas.drawLine(right, top, right, top + angleSize, paint);

        //左下角
        canvas.drawLine(left, bottom, left + angleSize, bottom, paint);
        canvas.drawLine(left, bottom, left, bottom - angleSize, paint);

        //右下角
        canvas.drawLine(right, bottom, right - angleSize, bottom, paint);
        canvas.drawLine(right, bottom, right, bottom - angleSize, paint);


        if (isScanning){
            if (autoX >= height) {
                autoX = 0;
            } else {
                autoX += speed;
            }
            paint.setStrokeWidth(scanningLineSize);
            paint.setColor(scanningLineColor);
            canvas.drawLine(0, autoX, width, autoX, paint);
        }
    }
}
